#include<pthread.h>
	#include<stdio.h>
	#define false 0
	#define true 1
	int flag[2];
	int turn;
	#define P_NUMBER 10 //并发线程数量
	void p0(){
		while(true){
			flag[0] = true;
			turn = 1;
			while(flag[1] &&  turn ==1){};
			printf("线程0成功进入了临界区\n");
			flag[0] = false;
			printf("线程0推出了缓冲区\n");
		}
		pthread_exit(0);
	}
	void p1(){
		while(true){
			flag[1]=true;
			turn = 0;
			while(flag[0] && turn ==0);
			printf("线程1成功进入了临界区\n");
			flag[1] = false;
			printf("线程1推出了缓冲区\n");
		}
		pthread_exit(0);	
	}
	int main(){
		flag[0]=flag[1]=false;
		pthread_t pid[P_NUMBER];//线程数组
		int i = 0;
		for(;i<P_NUMBER;i++){
			if(i%2){
				
			pthread_create(&pid[i],NULL,(void *)p1,NULL);	
			}	
			else{
			pthread_create(&pid[i],NULL,(void *)p0,NULL);
			}
		}
		i=0;
		for(;i<P_NUMBER;i++){
			pthread_join(pid[i],NULL);//回收线程
		}
		printf("执行完毕");
		return 0;
	}
